<?
class DB{
private $dbhost="dbhost"; //default host and port
private $dbuser="dbuser";//default mysql user
private $dbpass="dbpass";//default mysql passwd
private $dbname="dbname";//default database

function __construct($hostport,$username,$password,$database){
	$this->dbhost=$hostport;
	$this->dbuser=$username;
	$this->dbpass=$password;
	$this->dbname=$database;
}

public function selectSQL($sql,$option){
	$link = mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or exit("Cannot connect to the db server!");
	$tof1 = mysql_select_db($this->dbname) or exit("Cannot select the database!");
	$sql0 = "set names utf8";
	$sql1 = $sql;
	$res0 = mysql_query($sql0) or exit ("Cannot execute the query sql0!");
	$res1 = mysql_query($sql1) or exit ("Cannot execute the query sql1!");
	
	switch($option){
		case 'noReturn': return $res1; break;//execute non-query operation
		case 'defaultQuery':{
			$result=array();
			while($record=mysql_fetch_row($res1)){ 
				$result[]=$record;
			}
			$tof2=mysql_free_result($res1) or exit("Cannot free the result!");
			return $result;
		}break;
		//other case added here
		default: break;
	}
}

public function insert($sql){ return $this->selectSQL($sql,'noReturn'); }

public function delete($sql){ return $this->selectSQL($sql,'noReturn'); }

public function update($sql){ return $this->selectSQL($sql,'noReturn'); }

public function select($sql){ return $this->selectSQL($sql,'defaultQuery'); }

}

$db=new DB(dbhost,dbuser,dbpass,dbname);
//数据类
?>